package com.bwf.lock;

import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/20 15:08
 * @Copyright 博为峰
 */
public class LockDemo02 {
    public static Object objA = new Object();
    public static Object objB = new Object();

    public static void main(String[] args) {
        new Thread(new LockA()).start();
        new Thread(new LockB()).start();
    }
}

class LockA implements Runnable{
    @Override
    public void run() {
        System.out.println("LockA开始执行");
        while (true){
           synchronized (LockDemo02.objA){
               System.out.println("LockA获取了锁 objA");
               try {
                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               synchronized (LockDemo02.objB){
                   System.out.println("LockA锁住了 objB");
               }
           }
        }
    }
}

class LockB implements Runnable{
    @Override
    public void run() {
        System.out.println("LockB开始执行");
        while (true){
            synchronized (LockDemo02.objB){
                System.out.println("LockB获取了锁 objB");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (LockDemo02.objA){
                    System.out.println("LockB锁住了 objA");
                }
            }
        }
    }
}
